﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.SysManage.UniualMateral
{
    public partial class Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                VerdictIsLogin();
                if (!Page.IsPostBack)
                {
                    this.txtSign.Text = Request["sign"];
                    BianDropList();
                    if (this.txtSign.Text == "0")  //添加
                    {
                        if (Request["pid"] != null)
                        {
                            string strMonth = Request["pid"];
                            dropMonth.SelectedValue = strMonth;
                        }
                        if (Request["pricid"]!=null)
                        {
                            dropPrice.SelectedValue=Request["pricid"];
                        }
                    }

                    if (this.txtSign.Text.Trim() == "1")  //编辑
                    {
                        this.txtIdHid.Text = Request["id"];
                        fillForm();
                    }

                }
            }
            catch (Exception ex)
            {
                Universal.JS.Alert(ex.Message);
            }
        }

        private void BianDropList()
        {
            BLL.SysManage.T_PUB_DICTIONARY dicBll = new BLL.SysManage.T_PUB_DICTIONARY();
            List<Model.SysManage.T_PUB_DICTIONARY> list = dicBll.GetListArray(" parent_id='6'");
            dropPrice.DataSource = list;
            dropPrice.DataTextField = "name";
            dropPrice.DataValueField = "id";
            dropPrice.DataBind();
            dropPrice.Items.Insert(0, new ListItem("--请选择--", ""));

        }

        #region 填充页面数据
        protected void fillForm()
        {
            string strid = txtIdHid.Text;
            BLL.SysManage.T_PUB_UNIALMATIAL bll = new BLL.SysManage.T_PUB_UNIALMATIAL();
            Model.SysManage.T_PUB_UNIALMATIAL model = bll.GetModel(Convert.ToInt32(strid));
            if (model != null)
            {
                string strMonth = model.Month;
                if (dropMonth.Items.FindByValue(strMonth) != null)
                {
                    dropMonth.SelectedValue = strMonth;
                }
                string strWeedPrice = model.WeedPrice;
                if (dropPrice.Items.FindByValue(strWeedPrice)!=null)
                {
                    dropPrice.SelectedValue = strWeedPrice;
                }

                txtHidMID.Text = model.Material.ToString();

                BLL.SysManage.T_PUB_MATERIAL materialBll = new BLL.SysManage.T_PUB_MATERIAL();
                Model.SysManage.T_PUB_MATERIAL materialModel = materialBll.GetModel(Convert.ToInt32(model.Material));
                txt_name.Text = materialModel.MATERIAL_NAME;
                txtPrice.Text = model.Price.ToString();
                txtOther.Text = model.Other;
            }
        }
        #endregion


        #region 提交按钮事件
        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.SysManage.T_PUB_UNIALMATIAL bll = new BLL.SysManage.T_PUB_UNIALMATIAL();
                Model.SysManage.T_PUB_UNIALMATIAL model = new Model.SysManage.T_PUB_UNIALMATIAL();
                model.Material = Convert.ToInt32(txtHidMID.Text.Trim());
                model.Month = dropMonth.SelectedValue;
                model.Other = txtOther.Text;
                if (txtPrice.Text.Trim()!="")
                {
                    model.Price = Convert.ToDecimal(txtPrice.Text.Trim());
                }
                else
                {
                    model.Price = null;
                }
                model.WeedPrice = dropPrice.SelectedValue;

                if (this.txtSign.Text.Trim() == "0")
                {

                    bll.Add(model);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加 推荐物品" + txt_name.Text, DateTime.Now);
                }
                else if (this.txtSign.Text.Trim() == "1")
                {
                    model.id = Convert.ToInt32(txtIdHid.Text);
                    bll.Update(model);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "更新  推荐物品" + txt_name.Text, DateTime.Now);
                }
                //Universal.JS.Alert("保存成功！");
                Response.Redirect("List.aspx?id=" + dropMonth.SelectedValue, false);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }

        }
        #endregion

        protected void BtnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx?id=" + dropMonth.SelectedValue);
        }
    }
}